home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Interapplication Communication / MenuScripter 4.0 / Sources / MSAERecording.c < prev    next >
Encoding:
Text File  |  1996-07-09  |  30.9 KB  |  1,236 lines  |  [TEXT/CWIE]

  1. // MSAERecording.c
  2. //
  3. // Original version by Jon Lansdell and Nigel Humphreys.
  4. // 4.0 and 3.1 updates by Greg Sutton.
  5. // ©Apple Computer Inc 1996, all rights reserved.
  6.  
  7. /*
  8.     Changes for 3.1
  9.  
  10.     14-Nov-95 : GS : Disposed of style handle correctly in BuildStyledTextDesc().
  11.     14-Nov-95 : GS : Disposed of text object specifier in IssueSizeCommand(),
  12.                         IssueFontCommand() and IssueStyleCommand().
  13.                         
  14.     Changes for 4.0
  15.     
  16.     29-Feb-96 : GS : MakeWindowObj() makes a document object specifier if the window
  17.                         is a document.
  18. */
  19.  
  20. #include "MSAERecording.h"
  21.  
  22. #include "MSAEUtils.h"
  23. #include "MSUtils.h"
  24. #include "MSAEWindowUtils.h"
  25. #include "MSAETextUtils.h"
  26. #include "MSAppleEvents.h"
  27. #include "MSAEGetData.h"
  28.  
  29. #include <AEPackObject.h>
  30.  
  31.  
  32.  
  33. static short   gBigBrother;
  34. static char    *gTypingBuffer;
  35. static short   gCharsInBuffer;
  36. static AEDesc  gTypingTargetObject;
  37.  
  38. const short    kTextSizeDialog = 1004;
  39.  
  40.  
  41. OSErr    InstallRecordingHandlers(void)
  42. {
  43.     OSErr    err;
  44.     
  45.     gBigBrother = 0;
  46.     gCharsInBuffer = 0;
  47.     gTypingBuffer = (char *)NewPtr(kMaxTELength);
  48.     gTypingTargetObject.dataHandle = 0;
  49.     
  50.     err = AEInstallEventHandler( kCoreEventClass, kAENotifyStartRecording, NewAEEventHandlerProc(HandleStartRecording), noRefCon, false);
  51.     err = AEInstallEventHandler( kCoreEventClass, kAENotifyStopRecording, NewAEEventHandlerProc(HandleStopRecording), noRefCon, false);
  52.  
  53.     return(err);
  54. }
  55.  
  56. pascal OSErr HandleStartRecording(const AppleEvent *theAppleEvent,
  57.                                                           AppleEvent *reply,
  58.                                                           long       handlerRefCon)        
  59. {
  60. #ifdef __MWERKS__
  61.     #pragma unused (reply,handlerRefCon)
  62. #endif
  63.  
  64.     OSErr myErr;
  65.  
  66.     gBigBrother++;
  67.  
  68.     myErr = GotRequiredParams(theAppleEvent);
  69.  
  70.     return(myErr);
  71.     
  72. } // HandleStartRecording
  73.  
  74. pascal OSErr HandleStopRecording(const AppleEvent *theAppleEvent,
  75.                                                          AppleEvent *reply,
  76.                                                              long handlerRefCon)        
  77. {
  78. #ifdef __MWERKS__
  79.     #pragma unused (theAppleEvent,reply,handlerRefCon)
  80. #endif
  81.  
  82.     gBigBrother--;
  83.     return(noErr);
  84. } // HandleStopRecording
  85.  
  86.  
  87. // Make an object specifier for a window given the WindowPtr
  88.  
  89. OSErr    MakeWindowObj(WindowPtr theWindow, AEDesc *result)
  90. {
  91.     AEDesc        nullDesc = {typeNull, NULL},
  92.                 aDesc = {typeNull, NULL};
  93.     long        anIndex;
  94.     Str255        aName;
  95.     DescType    keyForm;
  96.     OSErr        err;
  97.     
  98.     if ( ! theWindow )
  99.         return errAENoSuchObject;
  100.         
  101.     if ( IsDocumentWindow( theWindow ) )
  102.         return MakeDocumentObj( theWindow, result );
  103.     
  104.     if ( IsVisible( theWindow ) )
  105.     {
  106.         anIndex = GetWindowIndex(theWindow);
  107.         if ( ! anIndex )
  108.             return errAENoSuchObject;
  109.         
  110.         err = AECreateDesc( typeLongInteger, (Ptr)&anIndex,
  111.                                     sizeof( anIndex ), &aDesc );
  112.         if (noErr != err) goto done;
  113.         
  114.         keyForm = formAbsolutePosition;
  115.     }
  116.     else
  117.     {
  118.         GetWTitle( theWindow, aName );
  119.         
  120.         err = AECreateDesc( typeChar, (Ptr)&aName[1],
  121.                                             aName[0], &aDesc );
  122.         if (noErr != err) goto done;
  123.         
  124.         keyForm = formName;
  125.     }
  126.     
  127.     err = CreateObjSpecifier( cWindow, &nullDesc, keyForm,
  128.                                                 &aDesc, false, result );
  129.  
  130. done:
  131.     (void)AEDisposeDesc( &aDesc );
  132.     
  133.     return(err);
  134. } // MakeWindowObj
  135.  
  136. // Make an object specifier for a document given the WindowPtr
  137.  
  138. OSErr    MakeDocumentObj(WindowPtr theWindow, AEDesc *result)
  139. {
  140.     AEDesc        nullDesc = {typeNull, NULL},
  141.                 aDesc = {typeNull, NULL};
  142.     long        anIndex;
  143.     Str255        aName;
  144.     DescType    keyForm;
  145.     OSErr        err;
  146.     
  147.     if ( ! theWindow )
  148.         return errAENoSuchObject;
  149.         
  150.     if ( ! IsDocumentWindow( theWindow ) )
  151.         return errAEWrongDataType;
  152.     
  153.     if ( IsVisible( theWindow ) )
  154.     {
  155.         anIndex = GetDocumentIndex(theWindow);
  156.         if ( ! anIndex )
  157.             return errAENoSuchObject;
  158.         
  159.         err = AECreateDesc( typeLongInteger, (Ptr)&anIndex,
  160.                                     sizeof( anIndex ), &aDesc );
  161.         if (noErr != err) goto done;
  162.         
  163.         keyForm = formAbsolutePosition;
  164.     }
  165.     else
  166.     {
  167.         GetWTitle( theWindow, aName );
  168.         
  169.         err = AECreateDesc( typeChar, (Ptr)&aName[1],
  170.                                             aName[0], &aDesc );
  171.         if (noErr != err) goto done;
  172.         
  173.         keyForm = formName;
  174.     }
  175.     
  176.     err = CreateObjSpecifier( cDocument, &nullDesc, keyForm,
  177.                                                 &aDesc, false, result );
  178.  
  179. done:
  180.     (void)AEDisposeDesc( &aDesc );
  181.     
  182.     return(err);
  183. } // MakeDocumentObj
  184.  
  185.  
  186. OSErr    MakeTextObjFromToken(TextToken* theToken, AEDesc* result)
  187. {
  188.     OSErr    err;
  189.  
  190.     err = GetTextTokenObjectSpecifier(theToken, result);
  191.     
  192.     return(err);
  193. }
  194.  
  195.  
  196. OSErr    MakeSelectedTextObj(WindowPtr theWindow, TEHandle theTextEditHandle, AEDesc* result)
  197. {
  198.     return( MakeTextObj(theWindow, (**theTextEditHandle).selStart,
  199.                             (**theTextEditHandle).selEnd, result));
  200. } // MakeSelectedTextObj
  201.  
  202.  
  203. OSErr    MakeTextObj(WindowPtr theWindow, short selStart, short selEnd, AEDesc* result)
  204. {
  205.     TextToken    aToken;
  206.     OSErr        err;
  207.     
  208.     aToken.tokenWindow = theWindow;
  209.     aToken.tokenOffset = selStart + 1;
  210.     aToken.tokenLength = selEnd - selStart;
  211.  
  212.     err = MakeTextObjFromToken(&aToken, result);
  213.  
  214.     return(err);
  215. }
  216.  
  217. OSErr    SendSelectionEvent(DPtr docPtr)
  218. {
  219.     AEAddressDesc    ourAddress = {typeNull, NULL};
  220.     AppleEvent        selectEvent = {typeNull, NULL},
  221.                     ignoreReply = {typeNull, NULL};
  222.     AEDesc            textObj = {typeNull, NULL};
  223.     OSErr            err;
  224.  
  225.  
  226.     err = MakeSelfAddress(&ourAddress);
  227.     if (noErr != err) goto done;
  228.     
  229.         // Build an object to represent the current document's selection
  230.         // MakeSelectedTextObj
  231.     err = MakeSelectedTextObj(docPtr->theWindow, docPtr->theText, &textObj);
  232.     if (noErr != err) goto done;
  233.     
  234.     err = AECreateAppleEvent(kAEMiscStandards, kAESelect, &ourAddress, 0, 0, &selectEvent);    
  235.     if (noErr != err) goto done;
  236.     
  237.         // add parameter
  238.     err = AEPutParamDesc(&selectEvent, keyDirectObject, &textObj);
  239.     if (noErr != err) goto done;
  240.                     
  241.         // and now send the message
  242.     err = AESend(&selectEvent, &ignoreReply, kAENoReply, kAEHighPriority, kAEDefaultTimeout, NULL, NULL);
  243.     if (noErr != err) goto done;
  244.  
  245. done:    
  246.     (void)AEDisposeDesc(&ourAddress);
  247.     (void)AEDisposeDesc(&selectEvent);
  248.     (void)AEDisposeDesc(&ignoreReply);
  249.     (void)AEDisposeDesc(&textObj);
  250.  
  251.     return(err);
  252. }
  253.  
  254. void    DoEditCommand(DPtr theDocument, editCommandType whatCommand)
  255. {
  256.     AEAddressDesc    ourAddress = {typeNull, NULL};
  257.     AppleEvent        editCommandEvent = {typeNull, NULL},
  258.                     ignoreReply = {typeNull, NULL};
  259.     AEEventID        theEventID;
  260.     AEEventClass    theEventClass;
  261.     OSErr            err;
  262.     
  263.     err = SendSelectionEvent(theDocument);
  264.     if (noErr != err) goto done;
  265.  
  266.         // Now create and send the appropriate cut, copy, paste or clear AppleEvent
  267.     
  268.     switch (whatCommand)
  269.     {
  270.         case  editCutCommand:
  271.             theEventID = kAECut;
  272.             theEventClass = kAEMiscStandards;
  273.             break;
  274.             
  275.         case  editCopyCommand:
  276.             theEventID = kAECopy;
  277.             theEventClass = kAEMiscStandards;
  278.             break;
  279.  
  280.         case  editPasteCommand:
  281.             theEventID = kAEPaste;
  282.             theEventClass = kAEMiscStandards;
  283.             break;
  284.  
  285.         case  editClearCommand:
  286.             theEventID = kAEDelete;
  287.             theEventClass = kAECoreSuite;
  288.             break;
  289.     }
  290.     
  291.     err = MakeSelfAddress(&ourAddress);
  292.     if (noErr != err) goto done;
  293.             
  294.     err = AECreateAppleEvent(theEventClass, theEventID, &ourAddress, 0, 0, &editCommandEvent);    
  295.     if (noErr != err) goto done;
  296.             
  297.         // and now Send the message
  298.     err = AESend(&editCommandEvent, &ignoreReply, kAENoReply, kAEHighPriority, kAEDefaultTimeout, NULL, NULL);
  299.         
  300. done:
  301.     (void)AEDisposeDesc(&ourAddress);
  302.     (void)AEDisposeDesc(&editCommandEvent);
  303.     (void)AEDisposeDesc(&ignoreReply);
  304.  
  305. } // DoEditCommand
  306.  
  307. void    IssueCutCommand(DPtr theDocument)
  308. {            
  309.     DoEditCommand(theDocument, editCutCommand);
  310.  
  311. void    IssueCopyCommand(DPtr theDocument)
  312. {
  313.     DoEditCommand(theDocument, editCopyCommand);
  314. }
  315.  
  316. void    IssuePasteCommand(DPtr theDocument)
  317. {
  318.     DoEditCommand(theDocument, editPasteCommand);    
  319. }
  320.  
  321. void    IssueClearCommand(DPtr theDocument)
  322. {
  323.     DoEditCommand(theDocument, editClearCommand);    
  324. }
  325.  
  326. // ---------------------------------------------------------------------
  327. //    Name :         SendAESetObjProp
  328. //    Function :    Creates a property object from an object,
  329. //                a property type and its data and sends it to
  330. //                the requested address, and cleans up zapping params too
  331. // ---------------------------------------------------------------------
  332.  
  333. OSErr    SendAESetObjProp(AEDesc *theObj, DescType theProp, AEDesc *theData, AEAddressDesc *toWhom)
  334. {
  335.     AEDesc        propObjSpec;
  336.     AppleEvent    myAppleEvent;
  337.     AppleEvent    defReply;
  338.     OSErr        myErr;
  339.     OSErr        ignoreErr;
  340.     AEDesc        theProperty;
  341.         
  342.     // create an object spec that represents the property of the given object
  343.     
  344.     myErr = AECreateDesc(typeType, (Ptr)&theProp, sizeof(theProp), &theProperty);
  345.     if (myErr==noErr)
  346.         myErr = CreateObjSpecifier(cProperty, theObj, formPropertyID,
  347.                                         &theProperty, true, &propObjSpec);    
  348.         
  349.     // create event
  350.     
  351.     if (myErr==noErr)
  352.         myErr = AECreateAppleEvent(kAECoreSuite, kAESetData, toWhom, 0, 0, &myAppleEvent);
  353.         
  354.     // add prop obj spec to the event
  355.     
  356.     if (myErr==noErr)
  357.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &propObjSpec);
  358.     
  359.     // add prop data to the event
  360.     
  361.     if (myErr==noErr)
  362.         myErr = AEPutParamDesc(&myAppleEvent,keyAEData, theData);
  363.     
  364.     // send event
  365.     
  366.     if (myErr==noErr)
  367.         myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract,
  368.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  369.     
  370.     if (myAppleEvent.dataHandle)
  371.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  372.         
  373.     if (&propObjSpec.dataHandle)
  374.       ignoreErr = AEDisposeDesc(&propObjSpec);
  375.     
  376.     if (theData->dataHandle)
  377.         ignoreErr = AEDisposeDesc(theData);
  378.     
  379.     if (toWhom->dataHandle)
  380.         ignoreErr = AEDisposeDesc(toWhom);
  381.     
  382.     return(myErr);
  383. } // SendAESetObjProp
  384.  
  385.  
  386. void    IssueFontCommand(DPtr theDocument, short theItem)
  387. {
  388.     Str255            name;
  389.     AEDesc            strDesc;
  390.     AEAddressDesc    theAddress;
  391.     AEDesc            selTextObj = {typeNull, NULL};
  392.     OSErr            err;
  393.         
  394.     err = MakeSelfAddress(&theAddress);
  395.     
  396.     err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  397.     
  398.     GetItem(myMenus[fontM], theItem, name);
  399.     
  400.     if (err==noErr)
  401.         err  = AECreateDesc(typeChar, (Ptr)&name[1], name[0], &strDesc);
  402.     
  403.     if (err==noErr)
  404.         err  = SendAESetObjProp(&selTextObj, pFont, &strDesc, &theAddress);
  405.         
  406.     (void)AEDisposeDesc(&selTextObj);                                            
  407. }
  408.  
  409. // Window property routines
  410.  
  411. void    IssueZoomCommand(WindowPtr whichWindow, short whichPart)
  412. {
  413.     Boolean       zoomBool;
  414.     AEDesc        zoomDesc;
  415.     AEAddressDesc selfAddr;
  416.     AEDesc        frontWinObj;
  417.     OSErr         err;
  418.  
  419.     err = MakeSelfAddress(&selfAddr);
  420.     
  421.     err = MakeWindowObj(whichWindow, &frontWinObj);
  422.     
  423.     zoomBool = (whichPart==inZoomOut);
  424.  
  425.     err = AECreateDesc(typeBoolean, (Ptr)&zoomBool, sizeof(zoomBool), &zoomDesc);
  426.                                             
  427.     err = SendAESetObjProp(&frontWinObj, pIsZoomed, &zoomDesc, &selfAddr);                                                            
  428. } // IssueZoomCommand
  429.  
  430. void    IssueCloseCommand(WindowPtr whichWindow)
  431. {
  432.     AEAddressDesc  selfAddr;
  433.     AEDesc         frontWinObj;
  434.     OSErr          err;
  435.     OSErr          ignoreErr;
  436.     AppleEvent     closeCommandEvent;
  437.     AppleEvent     ignoreReply;
  438.  
  439.     frontWinObj.dataHandle = nil;
  440.     
  441.     err = MakeSelfAddress(&selfAddr);
  442.     
  443.         // If it's a document this will be a document object sprcifier
  444.     err = MakeWindowObj(whichWindow, &frontWinObj);
  445.                                                         
  446.     err = AECreateAppleEvent( kAECoreSuite, kAEClose, &selfAddr,
  447.                 kAutoGenerateReturnID, kAnyTransactionID, &closeCommandEvent) ;                
  448.     
  449.     // add parameter - the window to close    
  450.     if (err==noErr) 
  451.         err = AEPutParamDesc(&closeCommandEvent, keyDirectObject, &frontWinObj);
  452.         
  453.     if (err==noErr) 
  454.         err = AESend( &closeCommandEvent, &ignoreReply, kAENoReply + kAEAlwaysInteract,
  455.                                          kAENormalPriority, kAEDefaultTimeout, NULL, NULL );
  456.     
  457.     if (closeCommandEvent.dataHandle) 
  458.         ignoreErr = AEDisposeDesc(&closeCommandEvent);
  459.     
  460.     if (selfAddr.dataHandle) 
  461.         ignoreErr = AEDisposeDesc(&selfAddr);
  462.         
  463.     if (frontWinObj.dataHandle) 
  464.         ignoreErr = AEDisposeDesc(&frontWinObj);
  465.         
  466. } // IssueCloseCommand
  467.  
  468. void    IssueSizeWindow(WindowPtr whichWindow, short newHSize, short newVSize)
  469. {
  470.     Rect          sizeRect;
  471.     Rect          contentRect;
  472.     short         edgeSize;
  473.     AEDesc        sizeDesc;
  474.     AEAddressDesc selfAddr;
  475.     AEDesc        frontWinObj;
  476.     OSErr         err;
  477.  
  478.     sizeRect    = (**(((WindowPeek)whichWindow)->strucRgn)).rgnBBox;
  479.     contentRect = (**(((WindowPeek)whichWindow)->contRgn)).rgnBBox;
  480.     
  481.     edgeSize = sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  482.     sizeRect.right = sizeRect.left+newHSize+edgeSize;
  483.     
  484.     edgeSize = sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  485.     sizeRect.bottom = sizeRect.top+newVSize+edgeSize;
  486.     
  487.     err = MakeSelfAddress(&selfAddr);
  488.     
  489.     err = MakeWindowObj(whichWindow, &frontWinObj);
  490.     
  491.     if (err==noErr)
  492.         err = AECreateDesc(typeQDRectangle, (Ptr)&sizeRect, sizeof(sizeRect), &sizeDesc);
  493.     
  494.     if (err==noErr)
  495.         err  = SendAESetObjProp(&frontWinObj, pBounds, &sizeDesc, &selfAddr);                                                            
  496. } // IssueSizeWindow
  497.  
  498. void    IssueMoveWindow(WindowPtr whichWindow, Rect* sizeRect)
  499. {
  500.     AEDesc            sizeDesc = { typeNull, NULL },
  501.                     frontWinObj = { typeNull, NULL };
  502.     AEAddressDesc    selfAddr = { typeNull, NULL };
  503.     OSErr            err;
  504.     
  505.     err = MakeSelfAddress(&selfAddr);
  506.     if ( noErr != err ) goto done;
  507.     
  508.     err = MakeWindowObj(whichWindow, &frontWinObj);
  509.     if ( noErr != err ) goto done;
  510.         
  511.     err = AECreateDesc( typeQDRectangle, (Ptr)sizeRect, sizeof( *sizeRect ), &sizeDesc );
  512.     if ( noErr != err ) goto done;
  513.     
  514.     err = SendAESetObjProp(&frontWinObj, pBounds, &sizeDesc, &selfAddr);
  515.         
  516. done:
  517.     (void)AEDisposeDesc( &selfAddr );    // These three are disposed of in SendAESetObjProp()
  518.     (void)AEDisposeDesc( &frontWinObj );//  if it makes it there.
  519.     (void)AEDisposeDesc( &sizeDesc );
  520. } // IssueMoveWindow
  521.  
  522. void    IssuePageSetupWindow(WindowPtr whichWindow, TPrint thePageSetup)
  523. {
  524.     AEDesc        sizeDesc;
  525.     AEAddressDesc selfAddr;
  526.     AEDesc        frontWinObj;
  527.     OSErr         err;
  528.  
  529.     err = MakeSelfAddress(&selfAddr);
  530.     
  531.     err = MakeWindowObj(whichWindow, &frontWinObj);
  532.     
  533.     if (err==noErr)
  534.         err = AECreateDesc(typeTPrint, (Ptr)&thePageSetup, sizeof(thePageSetup), &sizeDesc);
  535.                                              
  536.     if (err==noErr)
  537.         err = SendAESetObjProp(&frontWinObj, pPageSetup, &sizeDesc, &selfAddr);                                                            
  538.                                                  
  539. } //IssuePageSetupWindow
  540.  
  541.  
  542. void IssueGXPageSetupWindow(WindowPtr whichWindow, gxJob thGXJob)
  543. {
  544.     AEDesc        sizeDesc;
  545.     AEAddressDesc selfAddr;
  546.     AEDesc        frontWinObj;
  547.     OSErr         err;
  548.     Handle        thePGXHandle;
  549.  
  550.     // Flatten the gxJob to a handle
  551.  
  552.     thePGXHandle = NewHandle(0);
  553.     GXFlattenJobToHdl(thGXJob, thePGXHandle);
  554.  
  555.     err = MakeSelfAddress(&selfAddr);
  556.     
  557.     err = MakeWindowObj(whichWindow, &frontWinObj);
  558.     
  559.     if (err==noErr)
  560.         err = AECreateDesc(typeTPrint, (Ptr)*thePGXHandle,
  561.                                 GetHandleSize (thePGXHandle), &sizeDesc);
  562.                                              
  563.     if (err==noErr)
  564.         err = SendAESetObjProp(&frontWinObj, pGXPageSetup, &sizeDesc, &selfAddr);                                                            
  565.  
  566.     DisposeHandle( thePGXHandle );                                             
  567. } // IssueGXPageSetupWindow
  568.  
  569.  
  570. void IssuePrintWindow(WindowPtr whichWindow, Boolean useDialog)
  571. {
  572.     AEAddressDesc selfAddr = { typeNull, NULL };
  573.     AEDesc        frontWinObj = { typeNull, NULL };
  574.     OSErr         err;
  575.     AppleEvent    printCommandEvent = { typeNull, NULL };
  576.     AppleEvent    ignoreReply;
  577.     AESendMode    sendModeFlags;
  578.  
  579.     err = MakeSelfAddress(&selfAddr);
  580.     
  581.     err = MakeWindowObj(whichWindow, &frontWinObj);
  582.                                                         
  583.     err = AECreateAppleEvent(kCoreEventClass, kAEPrintDocuments, &selfAddr, 0, 0, &printCommandEvent) ;                
  584.  
  585.     //    add parameter - the window to print
  586.  
  587.     if (err==noErr) 
  588.         err = AEPutParamDesc(&printCommandEvent, keyDirectObject, &frontWinObj);
  589.         
  590.     if (err==noErr)
  591.     {
  592.         sendModeFlags = kAENoReply;
  593.         if (useDialog)
  594.             sendModeFlags = sendModeFlags + kAEAlwaysInteract;
  595.         else
  596.             sendModeFlags = sendModeFlags + kAENeverInteract;
  597.         err = AESend(&printCommandEvent,&ignoreReply,sendModeFlags,kAEHighPriority,10000,nil, nil);
  598.     }
  599.     
  600.     (void)AEDisposeDesc(&printCommandEvent);
  601.     (void)AEDisposeDesc(&frontWinObj);
  602.     (void)AEDisposeDesc(&selfAddr);
  603. } // IssuePrintWindow
  604.  
  605. OSErr    IssueAEOpenDoc(FSSpec myFSSpec)
  606. // send OpenDocs AppleEvent to myself, with a one-element list
  607. // containing the given file spec
  608. //
  609. // NOTES : the core AEOpenDocs event is defined as taking a list of
  610. //        aliases (not file specs) as its direct parameter.  However,
  611. //        we can send the file spec instead and depend on AppleEvents'
  612. //        automatic coercion.  In fact, we don't really even have to put 
  613. //        in a list; AppleEvents will coerce a descriptor into a 1-element
  614. //        list if called for.  In this routine, though, we'll make the
  615. //        list for demonstration purposes.
  616. {
  617.     AppleEvent    myAppleEvent = { typeNull, NULL };
  618.     AppleEvent    defReply = { typeNull, NULL };
  619.     AEDescList    docList = { typeNull, NULL };
  620.     AEAddressDesc selfAddr = { typeNull, NULL };
  621.     OSErr         myErr;
  622.         
  623.     //    Create empty list and add one file spec
  624.  
  625.     myErr = AECreateList(nil,0,false, &docList);
  626.     
  627.     if (myErr==noErr) 
  628.         myErr = AEPutPtr(&docList,1,typeFSS,(Ptr)&myFSSpec,sizeof(myFSSpec));
  629.         
  630.     //    Create a self address to send it to
  631.  
  632.     if (myErr==noErr) 
  633.         myErr = MakeSelfAddress(&selfAddr);
  634.         
  635.     if (myErr==noErr) 
  636.         myErr = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &selfAddr,
  637.                                 kAutoGenerateReturnID,  kAnyTransactionID, &myAppleEvent);
  638.  
  639.     //    Put Params into our event and send it
  640.  
  641.     if (myErr == noErr) 
  642.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &docList);
  643.  
  644.     myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract, kAENormalPriority,
  645.                                                                      kAEDefaultTimeout, nil, nil);
  646.         
  647.     (void)AEDisposeDesc(&selfAddr);
  648.     (void)AEDisposeDesc(&myAppleEvent);
  649.     (void)AEDisposeDesc(&docList);
  650.         
  651.     return(myErr);
  652.     
  653. } // IssueAEOpenDoc
  654.  
  655. void    IssueAENewWindow(void)
  656. // send the New Element event to myself with a null container
  657. {
  658.     AppleEvent    myAppleEvent = { typeNull, NULL };
  659.     AppleEvent    defReply = { typeNull, NULL };
  660.     AEAddressDesc selfAddr = { typeNull, NULL };
  661.     OSErr         myErr;
  662.     DescType      elemClass;
  663.     
  664.     //    Create the address of us
  665.     
  666.     myErr = MakeSelfAddress(&selfAddr);
  667.     
  668.     //    create event 
  669.     
  670.     myErr = AECreateAppleEvent(kAECoreSuite, kAECreateElement, &selfAddr,
  671.                                     kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  672.     
  673.     //    attach desired class of new element
  674.     
  675.     elemClass = cDocument;
  676.     
  677.     if (myErr == noErr) 
  678.         myErr = AEPutParamPtr(&myAppleEvent, keyAEObjectClass, typeType,
  679.                                         (Ptr)&elemClass, sizeof(elemClass));
  680.         
  681.     //    send the event 
  682.     
  683.     if (myErr == noErr) 
  684.         myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAENeverInteract,
  685.                             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  686.     //    Clean up - reply never created so don't throw away
  687.  
  688.     (void)AEDisposeDesc(&selfAddr);
  689.     (void)AEDisposeDesc(&myAppleEvent);
  690.                 
  691. } // IssueAENewWindow
  692.  
  693. OSErr    IssueSaveCommand( WindowPtr theWindow, FSSpecPtr theSpec )
  694. // send an AppleEvent Save Event to myself
  695. {
  696.     AEDesc        windowObj = { typeNull, NULL };
  697.     AppleEvent    myAppleEvent = { typeNull, NULL };
  698.     AppleEvent    defReply = { typeNull, NULL };
  699.     AEAddressDesc selfAddr = { typeNull, NULL };
  700.     OSErr         anErr;
  701.     
  702.     anErr = MakeWindowObj(theWindow, &windowObj);
  703.     if ( noErr != anErr ) goto done;
  704.     
  705.     anErr = MakeSelfAddress(&selfAddr);
  706.     if ( noErr != anErr ) goto done;
  707.     
  708.         //    Build event
  709.  
  710.     anErr = AECreateAppleEvent(kAECoreSuite, kAESave, &selfAddr,
  711.                             kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  712.     if ( noErr != anErr ) goto done;
  713.   
  714.         //    say which window
  715.  
  716.     anErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  717.     if ( noErr != anErr ) goto done;
  718.  
  719.         //    add optional file param if we need to
  720.  
  721.     if ( theSpec ) 
  722.     {
  723.         anErr = AEPutParamPtr( &myAppleEvent, keyAEFile, typeFSS,
  724.                                                     (Ptr)theSpec, sizeof( FSSpec ) );
  725.         if ( noErr != anErr ) goto done;
  726.     }
  727.     
  728.         // send the event
  729.  
  730.     anErr = AESend( &myAppleEvent, &defReply, kAENoReply + kAENeverInteract,
  731.                                     kAENormalPriority, kAEDefaultTimeout, NULL, NULL );
  732.  
  733. done:      
  734.     (void)AEDisposeDesc(&selfAddr);
  735.     (void)AEDisposeDesc(&windowObj);
  736.     (void)AEDisposeDesc(&myAppleEvent);
  737.         
  738.     return anErr;
  739. }    // IssueSaveCommand
  740.  
  741. OSErr    IssueRevertCommand(WindowPtr theWindow)
  742.     // send an AppleEvent Revert Event to myself
  743. {
  744.     AEDesc        windowObj = { typeNull, NULL };
  745.     AppleEvent    myAppleEvent = { typeNull, NULL };
  746.     AppleEvent    defReply = { typeNull, NULL };
  747.     OSErr         myErr;
  748.     AEAddressDesc selfAddr = { typeNull, NULL };
  749.     
  750.     myErr = MakeWindowObj(theWindow, &windowObj);
  751.                 
  752.     if (myErr==noErr) 
  753.         myErr = MakeSelfAddress(&selfAddr);
  754.         
  755.     // Build event
  756.     
  757.     if (myErr == noErr) 
  758.         myErr  = AECreateAppleEvent(kAEMiscStandards, kAERevert, &selfAddr,
  759.                                     kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  760.     // say which window
  761.     
  762.     if (myErr == noErr) 
  763.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  764.     //    send the event
  765.  
  766.     if (myErr==noErr) 
  767.         myErr  = AESend(&myAppleEvent, &defReply, kAENoReply+kAENeverInteract,
  768.                                 kAENormalPriority, kAEDefaultTimeout, nil, nil);
  769.         
  770.     (void)AEDisposeDesc(&windowObj);
  771.     (void)AEDisposeDesc(&myAppleEvent);
  772.     (void)AEDisposeDesc(&selfAddr);
  773.         
  774.     return(myErr);
  775. } // IssueRevertCommand
  776.  
  777. // ----------------------------------------------------
  778. //    Name :         IssueQuitCommand
  779. //    Purpose :    Sends self a Quit AppleEvent
  780. // ----------------------------------------------------
  781. OSErr    IssueQuitCommand(void)
  782. {
  783.     AppleEvent    myAppleEvent = { typeNull, NULL };
  784.     AppleEvent    defReply = { typeNull, NULL };
  785.     OSErr         myErr;
  786.     AEAddressDesc selfAddr = { typeNull, NULL };
  787.     DescType      mySaveOpt;
  788.                         
  789.     myErr = MakeSelfAddress(&selfAddr);
  790.         
  791.     //    Build event
  792.     
  793.     if (myErr == noErr) 
  794.         myErr  = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &selfAddr,
  795.                                         kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  796.     //    say which save option
  797.     
  798.     mySaveOpt = kAEAsk;
  799.     
  800.     if (myErr == noErr) 
  801.         myErr = AEPutParamPtr(&myAppleEvent, keyAESaveOptions, typeEnumerated,
  802.                                                 (Ptr)&mySaveOpt, sizeof(mySaveOpt));
  803.     //    send the event
  804.  
  805.     if (myErr==noErr) 
  806.         myErr  = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract,
  807.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  808.                     
  809.     (void)AEDisposeDesc(&myAppleEvent);
  810.     (void)AEDisposeDesc(&selfAddr);
  811.         
  812.     return(myErr);
  813. } // IssueQuitCommand
  814.  
  815. #define kOK 1
  816. #define kCancel 2
  817. #define kOtherSize 4
  818. #define kOutlineItem 5
  819.  
  820. Boolean    PoseSizeDialog(long *whatSize)
  821. {
  822.     GrafPtr   savedPort;
  823.     DialogPtr aDialog;
  824.     Str255    aString;
  825.     short     itemHit;
  826.  
  827.     GetPort(&savedPort);
  828.     aDialog = GetNewDialog( kTextSizeDialog, NULL, (WindowPtr)-1 );
  829. //    ShowWindow(aDialog);
  830. //    SetPort(aDialog);
  831.     
  832.     AdornDefaultButton( aDialog, kOutlineItem );
  833.      
  834.         //set the edittext button to contain the right size
  835.     NumToString( *whatSize, aString );
  836.     SetText( aDialog, kOtherSize, aString );
  837.  
  838.     do
  839.     {
  840.         ModalDialog( nil, &itemHit );
  841.     } while ( ( itemHit != kOK ) && ( itemHit != kCancel ) );
  842.     
  843.     if ( itemHit == kOK ) 
  844.         RetrieveText( aDialog, kOtherSize, aString );
  845.  
  846.     DisposDialog( aDialog );
  847.     SetPort( savedPort );
  848.     
  849.     if ( itemHit == kOK ) 
  850.     {
  851.         // set the new size of the text
  852.         StringToNum( aString, whatSize );
  853.         if ( ( *whatSize < 1 ) || ( *whatSize > 2000 ) ) 
  854.             *whatSize = 12;
  855.     }
  856.  
  857.     return( itemHit == kOK );
  858. }
  859.  
  860. void    IssueSizeCommand(DPtr theDocument,short theItem)
  861. {
  862.     Str255        name;
  863.     AEDesc        sizeDesc = { typeNull, NULL };
  864.     AEAddressDesc theAddress = { typeNull, NULL };
  865.     OSErr         err;
  866.     AEDesc        selTextObj = { typeNull, NULL };
  867.     
  868.         // Vars to do with menu processing
  869.     short     lastSize;
  870.     short     upItem;
  871.     short     downItem;
  872.     short     otherItem;
  873.     long      theSize;
  874.     TextStyle theStyle;
  875.     short     lineHeight;
  876.     short     fontAscent;
  877.         
  878.     err = MakeSelfAddress(&theAddress);
  879.     
  880.     err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);        
  881.  
  882.         // check if the item is on the Size menu
  883.         // remembering that we can add and delete items from it
  884.     lastSize  = CountMItems(myMenus[sizeM]) - 5;
  885.     upItem    = lastSize + 2;
  886.     downItem  = upItem + 1;
  887.     otherItem = downItem + 2;
  888.   
  889.     TEGetStyle((**(theDocument->theText)).selStart, &theStyle, &lineHeight,
  890.                                             &fontAscent, theDocument->theText);
  891.     
  892.     GetItem(myMenus[sizeM], theItem, name);
  893.  
  894.     if (theItem <= lastSize)
  895.     {
  896.         GetItem(myMenus[sizeM], theItem, name);
  897.         StringToNum(name, &theSize);
  898.     }
  899.     else if (theItem == upItem)  
  900.         theSize = theStyle.tsSize + 1;
  901.     else if (theItem == downItem)
  902.         theSize = theStyle.tsSize - 1;
  903.     else if (theItem == otherItem) 
  904.     {
  905.         theSize = theStyle.tsSize;
  906.         if (!PoseSizeDialog(&theSize))
  907.             return;
  908.     }
  909.  
  910.     if (err==noErr)
  911.         err = CreateOffsetDescriptor(theSize, &sizeDesc);
  912.     
  913.     if (err==noErr)
  914.         err = SendAESetObjProp(&selTextObj, pPointSize, &sizeDesc, &theAddress);
  915.         
  916.     (void)AEDisposeDesc(&selTextObj);                                            
  917. } // IssueSizeCommand
  918.  
  919. void    IssueStyleCommand(DPtr theDocument, short theItem)
  920. {
  921.     Style         theFace;
  922.     OSErr         err;
  923.     AEDesc        result = { typeNull, NULL };
  924.     AEAddressDesc selfAddr = { typeNull, NULL };
  925.     AEDesc        selTextObj = { typeNull, NULL };
  926.     TextStyle     theStyle;
  927.     short         lineHeight;
  928.     short         fontAscent;
  929.         
  930.     TEGetStyle((**(theDocument->theText)).selStart, &theStyle,
  931.                     &lineHeight, &fontAscent, theDocument->theText);
  932.     
  933.     theFace = 0;
  934.  
  935.     switch (theItem)
  936.     {
  937.         case cPlain:
  938.             theFace = 0;
  939.             break;
  940.         case cBold:
  941.             theFace = bold;
  942.             break;
  943.         case cItalic:
  944.             theFace = italic;
  945.             break;
  946.         case cUnderline:
  947.             theFace = underline;
  948.             break;
  949.         case cOutline:
  950.             theFace = outline;
  951.             break;
  952.         case cShadow:
  953.             theFace = shadow;
  954.             break;
  955.         case cCondense:
  956.             theFace = condense;
  957.             break;
  958.         case cExtend:
  959.             theFace = extend;
  960.             break;
  961.     } // of switch
  962.  
  963.     if (theFace==0)
  964.         err = BuildTypeTextStylesDesc(0, bold+italic+underline+outline+shadow+condense+extend, &result);
  965.     else if (theFace & theStyle.tsFace)
  966.         err = BuildTypeTextStylesDesc(0, theFace, &result);
  967.     else
  968.         err = BuildTypeTextStylesDesc(theFace, 0, &result);
  969.     
  970.     err = MakeSelfAddress(&selfAddr);
  971.     
  972.     if (err==noErr)
  973.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  974.     
  975.     if (err==noErr)
  976.         err = SendAESetObjProp(&selTextObj, pTextStyles, &result, &selfAddr);
  977.         
  978.     (void)AEDisposeDesc(&selTextObj);                                            
  979. } // IssueStyleCommand
  980.  
  981.  
  982. OSErr    IssueSetDataObjToBufferContents(const AEDesc* theObj)
  983.     OSErr            myErr;
  984.     AEAddressDesc    theAddress = { typeNull, NULL };
  985.     AppleEvent        myAppleEvent = { typeNull, NULL };
  986.     AppleEvent        defReply = { typeNull, NULL };
  987.  
  988.     myErr = MakeSelfAddress(&theAddress);
  989.     
  990.     // create event
  991.     
  992.     if (myErr==noErr)
  993.         myErr = AECreateAppleEvent(kAECoreSuite, kAESetData, &theAddress,
  994.                                                         0, 0, &myAppleEvent);
  995.         
  996.     // add prop obj spec to the event
  997.     
  998.     if (myErr==noErr)
  999.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, theObj);
  1000.     
  1001.     // add prop data to the event
  1002.     
  1003.     if (myErr==noErr)
  1004.         myErr = AEPutParamPtr(&myAppleEvent, keyAEData, typeChar,
  1005.                                     (Ptr)gTypingBuffer, gCharsInBuffer);
  1006.                                                     
  1007.     // send event
  1008.     
  1009.     if (myErr==noErr)
  1010.      if (gRecordingImplemented)
  1011.          myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEDontExecute,
  1012.                                      kAENormalPriority, kAEDefaultTimeout, nil, nil);
  1013.  
  1014.     (void)AEDisposeDesc(&theAddress);
  1015.     (void)AEDisposeDesc(&myAppleEvent);
  1016.         
  1017.     return(myErr);
  1018. }
  1019.  
  1020. void    AddKeyToTypingBuffer(DPtr theDocument, char theKey)
  1021. {
  1022.     OSErr myErr;
  1023.     OSErr ignoreErr;
  1024.     
  1025.     if (theKey==BS || theKey==FS || theKey==GS || theKey==RS || theKey==US)
  1026.     {
  1027.         FlushAndRecordTypingBuffer();
  1028.         if (theKey==BS)
  1029.         {
  1030.             if ((**theDocument->theText).selStart!=(**theDocument->theText).selEnd)
  1031.             {
  1032.                 myErr = MakeTextObj(theDocument->theWindow,
  1033.                                     (**theDocument->theText).selStart,
  1034.                                     (**theDocument->theText).selEnd,
  1035.                                     &gTypingTargetObject);
  1036.             }
  1037.             else
  1038.             {
  1039.                 myErr = MakeTextObj(theDocument->theWindow,
  1040.                                     (**theDocument->theText).selStart-1,
  1041.                                     (**theDocument->theText).selStart,
  1042.                                     &gTypingTargetObject);
  1043.             }
  1044.                 
  1045.             myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  1046.             
  1047.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  1048.             
  1049.             gTypingTargetObject.dataHandle = nil;
  1050.         }
  1051.     }
  1052.     else
  1053.     {
  1054.         if (gCharsInBuffer==0)
  1055.             myErr = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText,
  1056.                                                                     &gTypingTargetObject);
  1057.  
  1058.         gTypingBuffer[gCharsInBuffer++] = theKey;
  1059.     }
  1060. }
  1061.  
  1062. void    FlushAndRecordTypingBuffer(void)
  1063.     OSErr  myErr;
  1064.     OSErr  ignoreErr;
  1065.  
  1066.     if (gCharsInBuffer != 0)
  1067.     {
  1068.         myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  1069.         
  1070.         if (gTypingTargetObject.dataHandle)
  1071.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  1072.     }
  1073.         
  1074.     gCharsInBuffer = 0;
  1075.     gTypingTargetObject.dataHandle = 0;
  1076. }
  1077.  
  1078.     
  1079. void    StyleTokConst(short theStyleItem, DescType *thekConst)
  1080. {
  1081.     switch (theStyleItem)
  1082.     {
  1083.         case bold:
  1084.             *thekConst = kAEBold;
  1085.             break;
  1086.             
  1087.         case italic:
  1088.             *thekConst = kAEItalic;
  1089.             break;
  1090.             
  1091.         case underline:
  1092.             *thekConst = kAEUnderline;
  1093.             break;
  1094.             
  1095.         case outline:
  1096.             *thekConst = kAEOutline;
  1097.             break;
  1098.             
  1099.         case shadow:
  1100.             *thekConst = kAEShadow;
  1101.             break;
  1102.             
  1103.         case condense:
  1104.             *thekConst = kAECondensed;
  1105.             break;
  1106.             
  1107.         case extend:
  1108.             *thekConst = kAEExpanded;
  1109.             break;
  1110.     }
  1111. } // StyleTokConst
  1112.  
  1113. OSErr    BuildTypeTextStylesDesc(Style onStyles, Style offStyles, AEDesc *resultDesc)
  1114. {
  1115.  
  1116.     OSErr     myErr;
  1117.     short     myStyleItem;
  1118.     DescType  styleConst;
  1119.     AEDesc    onStylesDesc = { typeNull, NULL };
  1120.     AEDesc    offStylesDesc = { typeNull, NULL };
  1121.     AEDesc    dataDesc = { typeNull, NULL };
  1122.  
  1123.     myErr = AECreateList(nil, 0, true,  &dataDesc);
  1124.     
  1125.     myErr = AECreateList(nil, 0, false, &onStylesDesc);
  1126.     myErr = AECreateList(nil, 0, false, &offStylesDesc);
  1127.     
  1128.     for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1129.         if (myErr==noErr)
  1130.         {
  1131.             StyleTokConst(myStyleItem, &styleConst);
  1132.             if (myStyleItem & onStyles)
  1133.                 myErr = AEPutPtr(&onStylesDesc,
  1134.                                     0,                // add to end of list
  1135.                                     typeEnumerated, // text for style name
  1136.                                     (Ptr)&styleConst,
  1137.                                     sizeof(styleConst));
  1138.             
  1139.             if (myStyleItem & offStyles)
  1140.                 myErr = AEPutPtr(&offStylesDesc,
  1141.                                     0,                // add to end of list
  1142.                                     typeEnumerated, // text for style name
  1143.                                     (Ptr)&styleConst,
  1144.                                     sizeof(styleConst));
  1145.         }
  1146.     
  1147.     if (myErr==noErr)
  1148.         myErr = AEPutKeyDesc(&dataDesc, keyAEOnStyles,  &onStylesDesc);
  1149.     
  1150.     if (myErr==noErr)
  1151.         myErr = AEPutKeyDesc(&dataDesc, keyAEOffStyles, &offStylesDesc);
  1152.     
  1153.     if (myErr==noErr)
  1154.         myErr = AECoerceDesc(&dataDesc, typeTextStyles, resultDesc);
  1155.         
  1156.     (void)AEDisposeDesc(&onStylesDesc);
  1157.     (void)AEDisposeDesc(&offStylesDesc);
  1158.     (void)AEDisposeDesc(&dataDesc);
  1159.  
  1160.     return(myErr);
  1161. }
  1162.  
  1163. OSErr    BuildTextStylesDesc(Style theStyle, AEDesc *resultDesc)
  1164. {
  1165.     short     myStyleItem;
  1166.     Style     onStyles;
  1167.     Style     offStyles;
  1168.             
  1169.     onStyles  = 0;
  1170.     offStyles = 0;
  1171.     
  1172.     for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1173.     {
  1174.         if (myStyleItem & theStyle)
  1175.             onStyles  = onStyles  + myStyleItem;
  1176.         else
  1177.             offStyles = offStyles + myStyleItem;
  1178.     }
  1179.  
  1180.     return(BuildTypeTextStylesDesc(onStyles, offStyles, resultDesc));
  1181.     
  1182. } // BuildTextStylesDesc
  1183.         
  1184. OSErr    BuildStyledTextDesc(TEHandle theHTE, short start, short howLong, AEDesc *resultDesc)
  1185. {
  1186.     AEDesc       listDesc = { typeNull, NULL };
  1187.     short        oldSelStart;
  1188.     short        oldSelEnd;
  1189.     StScrpHandle myStScrpHandle;
  1190.     OSErr        myErr;
  1191.     
  1192.     oldSelStart = (**theHTE).selStart;
  1193.     oldSelEnd   = (**theHTE).selEnd;
  1194.     
  1195.     TESetSelect(start-1, start+howLong-2, theHTE);
  1196.     
  1197.     myErr = AECreateList(nil, 0, true,  &listDesc);
  1198.     
  1199.     HLock((Handle)(**theHTE).hText);
  1200.                                                      
  1201.     if (myErr == noErr)
  1202.         myErr = AEPutKeyPtr(&listDesc, keyAEText, typeChar,
  1203.                             (Ptr)&(*(**theHTE).hText)[start-1], howLong);
  1204.                                                 
  1205.     HUnlock((Handle)(**theHTE).hText);
  1206.     
  1207.     myStScrpHandle = GetStylScrap(theHTE);
  1208.     
  1209.     if (myStScrpHandle)
  1210.     {
  1211.         HLock((Handle)myStScrpHandle);
  1212.         
  1213.         if (myErr==noErr)
  1214.             myErr = AEPutKeyPtr(&listDesc, keyAEStyles, typeScrapStyles,
  1215.                                     (Ptr)*myStScrpHandle, GetHandleSize((Handle)myStScrpHandle));
  1216.             
  1217.         HUnlock((Handle)myStScrpHandle);
  1218.  
  1219.         DisposeHandle((Handle)myStScrpHandle);
  1220.     }    
  1221.     else
  1222.         myErr = AEPutKeyPtr(&listDesc, keyAEStyles, typeScrapStyles, (Ptr)nil, 0);
  1223.     
  1224.     if (myErr==noErr)
  1225.         myErr = AECoerceDesc(&listDesc, typeStyledText, resultDesc); // should be typeIntlText
  1226.     
  1227.     (void)AEDisposeDesc(&listDesc);
  1228.     
  1229.     TESetSelect(oldSelStart, oldSelEnd, theHTE);
  1230.     
  1231.     return(myErr);
  1232. }
  1233.